Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sub-events

Package Overview
Dependencies
Maintainers
0
Versions
89
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sub-events

Lightweight, strongly-typed events, with monitored subscriptions.

  • 1.10.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Strongly-Typed Events

Build Status

Lightweight, strongly-typed events, with monitored subscriptions.

  • Documentation: API + WiKi.
  • Compatible with all existing events - see Extras.

Install

npm i sub-events

Usage

  • On event-provider side:
import {SubEvent} from 'sub-events';

// creating a strongly-typed event: 
const e: SubEvent<string> = new SubEvent();

// triggering the event when needed:
e.emit('hello');

API: SubEvent, emit

  • On event-consumer side:
// subscribing to the event:
const sub = e.subscribe((data: string) => {
  // data = 'hello'
});

// cancel the subscription when no longer needed:
sub.cancel();

API: Subscription, subscribe, cancel

Monitoring Subscriptions

Class SubEventCount extends SubEvent with event onCount, to observe the number of subscriptions:

import {SubEventCount, ISubCountChange} from 'sub-events';

// creating a strongly-typed event:
const e: SubEventCount<string> = new SubEventCount();

e.onCount.subscribe((info: ISubCountChange) => {
    // number of subscriptions has changed;
    // info = {newCount, prevCount} 
});

// any subscription will trigger event onCount:
const sub = e.subscribe(data => {});

// cancelling a subscription will trigger onCount:
sub.cancel();

API: SubEventCount, onCount

Browser

When including directly from HTML, you can access all types under subEvents namespace:

<script src="./node_modules/sub-events/dist"></script>
<script>
    const e = new subEvents.SubEvent();
    e.subscribe(data => {
        // data received
    });
</script>

Note that pre-built script includes only the core library, without the Extras, which you can bundle yourself as needed.

Keywords

FAQs

Package last updated on 15 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc